package com.wky.org.service.Impl;

import com.wky.basic.Result.PageResult;
import com.wky.basic.service.impl.BaseServiceImpl;
import com.wky.org.VO.EmployeeVO;
import com.wky.org.mapper.DepartmentMapper;
import com.wky.org.mapper.EmployeeMapper;
import com.wky.org.pojo.Department;
import com.wky.org.pojo.Employee;
import com.wky.org.service.DepartmentService;
import com.wky.org.service.EmployeeService;
import com.wky.user.mapper.LoginInfoMapper;
import com.wky.user.pojo.LoginInfo;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;



    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        System.out.println( employee);
        if (employee.getDepartmentId() == null) {
            return null;
        }

        Department department = departmentMapper.findById(employee.getDepartmentId());
        String name = department.getName();

        employee.setDepartmentName(name);

        return employee;
    }


    /**
     * 分页查询员工
     * @param employee
     * @return
     */
    @Override
    public PageResult<Employee> findByPage(Employee employee) {
        //分页数据
        List<Employee> employees = employeeMapper.findByPage(employee);
        //总条数
        Integer total = employeeMapper.findTotal(employee);

        //封装返回
        return new PageResult<Employee>(total,employees);
    }

    /**
     * 添加员工
     */
    @Override
    public void add(Employee employee) {
        //添加logininfo
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(employee.getUsername());
        loginInfo.setPhone(employee.getPhone());
        loginInfo.setEmail(employee.getEmail());
        loginInfo.setSalt(employee.getSalt());
        loginInfo.setPassword(employee.getPassword());
        loginInfo.setType(0);
        loginInfo.setDisable(1);
        loginInfoMapper.add(loginInfo);

        //添加employee
        employee.setLogininfoId(loginInfo.getId());
        employeeMapper.add(employee);
    }

}
